/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_host_config_manager.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
SleHostConfigManager::SleHostConfigManager(std::string fileName)
{
    SLE_ASSERT_AND_RETURN(fileName.empty() == false);
    fileName_ = fileName;
    sleHostCfgMgr_ = std::make_unique<SleConfigManager>(fileName_);
    SLE_ASSERT_AND_RETURN_LOG(sleHostCfgMgr_ != NULL, "create sleHostCfgMgr_ fail");
    SLE_ASSERT_AND_RETURN(GetConfigFromValues() == SLE_NO_ERROR);
}

SleHostConfigManager::SleHostConfigManager()
{
    SLE_ASSERT_AND_RETURN(fileName_.empty() == false);
    sleHostCfgMgr_ = std::make_unique<SleConfigManager>(fileName_);
    SLE_ASSERT_AND_RETURN_LOG(sleHostCfgMgr_ != NULL, "create sleHostCfgMgr_ fail");
    SLE_ASSERT_AND_RETURN(GetConfigFromValues() == SLE_NO_ERROR);
}

SleHostConfigManager::~SleHostConfigManager()
{}

SleErrorCode SleHostConfigManager::GetHostName(std::string &hostName)
{
    SLE_ASSERT_AND_RETURN_RET(hostName_.empty() == false, SLE_ERR_NOT_READY);
    hostName = hostName_;
    return SLE_NO_ERROR;
}

SleErrorCode SleHostConfigManager::SetHostName(const std::string &hostName)
{
    SLE_ASSERT_AND_RETURN_RET(hostName.empty() == false, SLE_ERR_INVALID_PARAM);
    hostName_ = hostName;
    SleErrorCode ret = SetConfigToValues();
    SLE_ASSERT_AND_RETURN_RET(ret == SLE_NO_ERROR, ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleHostConfigManager::GetHostAddr(uint8_t *hostAddr)
{
    memcpy(hostAddr, hostAddr_, SLE_DEVICE_ADDRESS_LEN);
    return SLE_NO_ERROR;
}

SleErrorCode SleHostConfigManager::SetHostAddr(const uint8_t *hostAddr)
{
    memcpy(hostAddr_, hostAddr, SLE_DEVICE_ADDRESS_LEN);
    SleErrorCode ret = SetConfigToValues();
    SLE_ASSERT_AND_RETURN_RET(ret == SLE_NO_ERROR, ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleHostConfigManager::GetIsAutoEnableSle(bool &isAutoEnableSle)
{
    isAutoEnableSle = isAutoEnableSle_;
    return SLE_NO_ERROR;
}

SleErrorCode SleHostConfigManager::SetIsAutoEnableSle(bool isAutoEnableSle)
{
    isAutoEnableSle_ = isAutoEnableSle;
    SleErrorCode ret = SetConfigToValues();
    SLE_ASSERT_AND_RETURN_RET(ret == SLE_NO_ERROR, ret);
    return SLE_NO_ERROR;
}

SleErrorCode  SleHostConfigManager::GetLastHostState(SleDeviceState &hostState)
{
    hostState = lastHostState_;
    return SLE_NO_ERROR;
}

SleErrorCode  SleHostConfigManager::SetLastHostState(SleDeviceState hostState)
{
    lastHostState_ = (hostState == SLE_HOST_STATE_DISABLED) ? 0 : 1;
    SleErrorCode ret = SetConfigToValues();
    SLE_ASSERT_AND_RETURN_RET(ret == SLE_NO_ERROR, ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleHostConfigManager::GetConfigFromValues()
{
    SleConfigValueMap valueMap;
    SleErrorCode ret = sleHostCfgMgr_->GetValue(valueMap);
    SleConfigValue values = valueMap["sle_host_config"];
    SLE_ASSERT_AND_RETURN_RET(ret == SLE_NO_ERROR, ret);
    isAutoEnableSle_ = sleHostCfgMgr_->str2Bool(values["isAutoEnableSle"]);
    hostName_ = values["hostName"];
    std::string hostAddrStr;
    hostAddrStr = values["hostAddr"];
    uint8_t hostAddr[SLE_DEVICE_ADDRESS_LEN];
    Str2Mac(hostAddr, hostAddrStr.c_str());
    memcpy(hostAddr_, hostAddr, SLE_DEVICE_ADDRESS_LEN);
    lastHostState_ = (SleDeviceState)sleHostCfgMgr_->Str2Uint8(values["lastHostState"]);
    return SLE_NO_ERROR;
}

SleErrorCode SleHostConfigManager::SetConfigToValues()
{
    SleConfigValue values;
    values["isAutoEnableSle"] = isAutoEnableSle_ == true ? "true" : "false";
    values["hostName"] = hostName_;
    char hostAddrStr[SLE_DEVICE_ADDRESS_LEN *3];
    sprintf(hostAddrStr, MACSTR, MAC2STR(hostAddr_));
    values["hostAddr"] = hostAddrStr;
    values["lastHostState"] = lastHostState_ == SLE_HOST_STATE_ENABLED ? "0" : "1";
    SleConfigValueMap valueMap;
    valueMap["sle_host_config"] = values;
    SleErrorCode ret = sleHostCfgMgr_->SetValue(valueMap);
    SLE_ASSERT_AND_RETURN_RET(ret == SLE_NO_ERROR, ret);
    return SLE_NO_ERROR;
}

} // SleStandard
} // NearLink
} // OHOS
